<?php
Class Games extends CI_Model
{
	
	//private $tableName = "game";
	private $gameId;
	
	public function __construct() {
		parent::__construct();
		//$this->$gameId = is_string($this->session->userdata('gameId')!='Undefined') ? $this->session->userdata('gameId'): 0;
	}
	
	public function removeAllRevealedCards(){
		$CI =& get_instance();
        $CI->load->model('turns');
        $CI->load->model('talons');
        $CI->turns->removeData();
	}
	
	public function getInfo(){
		$this->db->where('id',$this->session->userdata('gameId'));
		return $this->db->get('game')->result();
	}
	
	public function setCurrentGameId($gameId){
		$this->session->set_userdata('gameId',$gameId);
	}

	public function getCurrentGameId(){
		return $this->session->userdata('gameId');
	}
	
	public function checkGameId($gameId){
		$this->db->where("id",$gameId);
		$query = $this->db->get('game');
		if(count($query->result()) == 0){
			return true; // not used
		}else{
			//TODO: this should notify user that the there was an error creating game..
			return false;// used
		}
	}
	
	public function getResult(){
		$CI =& get_instance();
        $CI->load->model('player');
		$p1_cardsHolding = count($CI->player->counCardsOnHand(1));
		$p2_cardsHolding = count($CI->player->counCardsOnHand(2));
		if($p1_cardsHolding==0){
			return 1;
		}else if($p2_cardsHolding==0){
			return 2;
		}else{
			// probably draw  :) hahaha
		}
		
	}
	
	public function endCurrentGame(){
		$this->db->query('delete from game;');
	}
	
	public function createNewMasterSuit(){
		$CI =& get_instance();
        $CI->load->model('talons');
       return $CI->talons->createNewMasterSuit();
	}
	
	public function createNewGame(){
		$this->endCurrentGame();
		$CI =& get_instance();
        $CI->load->model('talons');
		$CI->load->model('card');
		$data = array(
		   'started' => date("Y-m-d")
		);
		
		$this->db->insert('game', $data); 
		 $gameId = $this->db->insert_id();
		$this->setCurrentGameId($gameId);
		
         //create sets of cards on hands.
		 //$CI->talon->createNewSetPlayerCardOnhand(1);	// player 1
		 //$CI->talon->createNewSetPlayerCardOnhand(2);	// player 2
		 //revealed the 7th card to identify the master suit
		 //$talonCardRevealedByGameId = $CI->talon->getTalonCardRevealedByGameId(false,1); // get the first row only 
		 
		 //$talonId = $talonCardRevealedByGameId{0}->id;
		 //$masterSuitId = $talonCardRevealedByGameId{0}->game_id; // or the cardid itself
		 //$masterSuit = $CI->card->getSuitByCardId($masterSuitId);// get suit of card (clubs,hearts,diamons,spades) either one will be master suit
		 //$this->session->set_userdata('masterSuit',$masterSuit);// save mastersuit to access in future use
		 // sync values between talon and game table for master suit id
		 //$this->updateMasterSuitByGameId($masterSuitId);
		 //$CI->talon->createNewMasterSuit($talonId);
		 return $gameId;
	}	

	public function updateMasterSuitByGameId($cardId){
		$update = array("masterSuit"=>$cardId);
		$this->db->where("id",$this->session->userdata('gameId'));
		$this->db->update(game,$update);
	}
	
}
?>